/*
 *Create By：Z.B
 *@DATE 2019/3/18 17:25
 *@EMAIL: 2545375@qq.com
 */
'use strict';

const Service = require('egg').Service;

class RoleService extends Service{
  async create(payload){
    return this.ctx.model.Role.create(payload);
  }

  async destroy(_id){
    const {ctx, service } = this;
    const role = await service.role.find(_id);
    if(!role){
      ctx.throw(404,'role not found');
    }
    return ctx.model.Role.findByIdAndRemove(_id);
  }

  async update(_id, payload){
    const {ctx, service} = this;
    const role = await service.role.find(_id);
    if(!role){
      ctx.throw(404,'role not found');
    }
    return ctx.model.Role.findByIdAndUpdate(_id, payload);
  }

  async show(_id){
    const role  = await this.ctx.service.role.find(_id);
    if(!role){
      ctx.throw(404,'role not found');
    }
    return this.ctx.model.Role.findById(_id);
  }

  async index(payload){

  }

  async removes(values){
    return this.ctx.model.Role.remove({_id:{$in: values}});
  }

  // Commons======================================================================================================>
  async find(_id){
    return this.ctx.model.Role.findById(id);
  }
}

module.exports = RoleService;